<?php
require_once('includes/config.php');
require_once('includes/functions/func.global.php');
require_once('includes/classes/class.template_engine.php');
require_once('includes/lang/lang_'.$config['lang'].'.php');

// Start the session
session_start();

// Connect to the database
db_connect($config);

// Check if the user has a remember cookie set
checkremember($config);

// Get site categories
$cats = get_cats($config,$lang);

// If no page is specified set to first page
if(!isset($_GET['page']))
{
	$_GET['page'] = 1;
}
else
{
	$_GET['page'] = makeInt($_GET['page']);
}

if(!isset($_GET['cmd']))
{
	$_GET['cmd'] = 'dugg';
}

$userinfo = mysql_fetch_array(mysql_query("SELECT user_id,username,avatar FROM ".$config['db']['pre']."users WHERE user_id='".validate_input($_GET['id'])."' LIMIT 1"));

if(!isset($userinfo['user_id']))
{
	exit('Sorry, this user could not be found');
}

$avatar = getUserAvatar($_GET['id'],$config,false,array($userinfo['avatar']),'s');
$user_avatar = getUserAvatar($_GET['id'],$config,false,array($userinfo['avatar']),'m');

$dugg = array();
$dugg_where = '';
$dugg_where2= '';
$dugg_count = 0;

if($_GET['cmd'] == 'dugg')
{
	$query_result = mysql_query("SELECT story_id,user_id FROM ".$config['db']['pre']."digs WHERE user_id='".validate_input($userinfo['user_id'])."' ORDER BY time DESC LIMIT ".validate_input(($_GET['page']-1)*10).",10");
	while ($info = @mysql_fetch_array($query_result))
	{
		$dugg_count++;
		$avatars[$info['user_id']] = $info['user_id'];
	
		if($dugg_where == '')
		{
			$dugg_where = "story_id='".$info['story_id']."'";
		}
		else
		{
			$dugg_where.= " OR story_id='".$info['story_id']."'";
		}
	}
	
	$total[0] = mysql_num_rows(mysql_query("SELECT 1 FROM ".$config['db']['pre']."digs WHERE user_id='".validate_input($userinfo['user_id'])."'"));
}
elseif($_GET['cmd'] == 'commented')
{
	$query_result = mysql_query("SELECT story_id,user_id FROM `".$config['db']['pre']."comm` WHERE user_id='".validate_input($_GET['id'])."' GROUP BY user_id ORDER BY comment_time DESC LIMIT ".validate_input(($_GET['page']-1)*10).",10");
	while ($info = @mysql_fetch_array($query_result))
	{
		$dugg_count++;
		$avatars[$info['user_id']] = $info['user_id'];
	
		if($dugg_where == '')
		{
			$dugg_where = "story_id='".$info['story_id']."'";
		}
		else
		{
			$dugg_where.= " OR story_id='".$info['story_id']."'";
		}
	}
	
	$total[0] = mysql_num_rows(mysql_query("SELECT 1 FROM ".$config['db']['pre']."comm WHERE user_id='".validate_input($userinfo['user_id'])."'"));
}
elseif($_GET['cmd'] == 'submitted')
{
	$dugg_where = "user_id='".addslashes($userinfo['user_id'])."'";

	$total[0] = mysql_num_rows(mysql_query("SELECT 1 FROM ".$config['db']['pre']."stories WHERE user_id='".validate_input($userinfo['user_id'])."'"));
}

if($dugg_where != '')
{
	if($_GET['cmd'] == 'submitted')
	{
		$query_result = mysql_query("SELECT story_id,story_title,story_url,story_time,story_thumb,story_desc,story_cat,story_digs,story_comments,user_name,user_id FROM ".$config['db']['pre']."stories WHERE ".$dugg_where." ORDER BY story_time DESC LIMIT ".validate_input(($_GET['page']-1)*10).",10");
	}
	else
	{
		$query_result = mysql_query("SELECT story_id,story_title,story_url,story_time,story_thumb,story_desc,story_cat,story_digs,story_comments,user_name,user_id FROM ".$config['db']['pre']."stories WHERE ".$dugg_where." LIMIT ".$dugg_count);
	}
	while ($info = @mysql_fetch_array($query_result))
	{
		$info['story_title'] = stripslashes($info['story_title']);
		$info['story_desc'] = stripslashes($info['story_desc']);
	
		$dugg[$info['story_id']] = $info;
		$dugg[$info['story_id']]['dugg'] = 0;
		$dugg[$info['story_id']]['buried'] = 0;
		$dugg[$info['story_id']]['cat_title'] = $cats[$info['story_cat']]['cat_title'];
		$dugg[$info['story_id']]['cat_id'] = $info['story_cat'];
		$dugg[$info['story_id']]['ago'] = time_taken((time()-$info['story_time']));
		$dugg[$info['story_id']]['domain'] = getDomain($info['story_url']);
		if($_GET['cmd'] == 'submitted')
		{
			$dugg[$info['story_id']]['avatar'] = $avatar;
		}
		else
		{
			$dugg[$info['story_id']]['avatar'] = getUserAvatar($info['user_id'],$config,true,'','s');
		}		
		
		if($config['mod_rewrite'] == 1)
		{
			$dugg[$info['story_id']]['story_link'] = $config['site_url'].'stories/'.$info['story_id'].'/'.modrewriteurl($info['story_title']).'.html';
			$dugg[$info['story_id']]['cat_link'] = $config['site_url'].'view/'.$info['story_cat'].'/'.modrewriteurl($cats[$info['story_cat']]['cat_title']).'/1.html';
			$dugg[$info['story_id']]['user_link'] = $config['site_url'].'users/'.$info['user_id'].'/'.$info['user_name'].'/1.html';
		}
		else
		{
			$dugg[$info['story_id']]['story_link'] = $config['site_url'].'story.php?id='.$info['story_id'];
			$dugg[$info['story_id']]['cat_link'] = $config['site_url'].'cat.php?i='.$info['story_cat'];
			$dugg[$info['story_id']]['user_link'] = $config['site_url'].'profile.php?id='.$info['user_id'];
		}
		
		if($dugg_where2 == '')
		{
			$dugg_where2 = "story_id='".$info['story_id']."'";
		}
		else
		{
			$dugg_where2.= " OR story_id='".$info['story_id']."'";
		}
	}
	
	if(isset($_SESSION['duser']['id']))
	{
		if($dugg_where2 != '')
		{
			$query = "SELECT story_id FROM ".$config['db']['pre']."digs WHERE (user_id='".$_SESSION['duser']['id']."' AND (".$dugg_where2."))";
			$query_result = @mysql_query ($query) OR error(mysql_error(), __LINE__, __FILE__, 0, '', '');
			while ($info = @mysql_fetch_array($query_result))
			{
				$dugg[$info['story_id']]['dugg'] = 1;
			}
		}
	}
}

$page = new HtmlTemplate ('templates/' . $config['tpl_name'] . '/profile.html');
$page->SetLoop ('CATS', $cats);
$page->SetLoop ('DUGG', $dugg);
$page->SetParameter ('MAILBOX_EN',$config['mailbox_en']);
$page->SetParameter ('USER_AVATAR', $user_avatar);
$page->SetParameter ('USER_ID', $_GET['id']);
$page->SetParameter ('USERNAME', $userinfo['username']);
$page->SetParameter ('OVERALL_HEADER', create_header($config,$lang,$cats,$userinfo['username']));
$page->SetParameter ('OVERALL_FOOTER', create_footer($config,$lang));
if(isset($_SESSION['duser']['id']))
{
	$page->SetParameter ('LOGGEDIN', 1);
}
else
{
	$page->SetParameter ('LOGGEDIN', 0);
}
if($config['mod_rewrite'])
{
	if($_GET['cmd'] == 'dugg')
	{
		$page->SetLoop ('PAGES', pagenav2($total[0],$_GET['page'],10,'profile.php',1,$_GET['id'],$config,$userinfo['username'],$lang));
	}
	else
	{
		$page->SetLoop ('PAGES', pagenav($total[0],$_GET['page'],10,$config['site_url'].'profile.php?id='.$_GET['id'].'&cmd='.$_GET['cmd'],1,$lang));
	}
}
else
{
	$page->SetLoop ('PAGES', pagenav($total[0],$_GET['page'],10,$config['site_url'].'profile.php?id='.$_GET['id'].'&cmd='.$_GET['cmd'],1,$lang));
}
$page->CreatePageEcho($lang,$config);
?>